package com.aha.secondchapter.third;

import com.aha.secondchapter.first.MyQueue;
import com.aha.secondchapter.second.MyStack;

import java.util.Arrays;

/**
 * 小猫钓鱼
 * Created by wangxin on 2015/10/31.
 */
public class CatFishing2 {
    public static void main(String[] args) {
        MyQueue xiaoha = new MyQueue(new int[]{2,4,1,2,5,6});
        MyQueue xiaoheng = new MyQueue(new int[]{3,1,3,5,6,4});
        MyStack ms = new MyStack(new int[100]);
        int[] book = new int[10];
        System.out.println(Arrays.toString(book));
        while(!xiaoha.isEmpty() && !xiaoheng.isEmpty()) {
            //小哈先出牌 牌入栈
            int xhTemp = xiaoha.out();
            //判断是否能收牌;result>0代表在result这个位置到刚出牌的位置为赢得的牌插入手牌队列中
            if (book[xhTemp] > 0) {
                xiaoha.in(xhTemp);
                while (ms.getTopData() != xhTemp) {
                    book[ms.getTopData()] = 0;//取消收走牌的标记
                    xiaoha.in(ms.pop());
                }
            }else{
                ms.push(xhTemp);
                book[xhTemp] = 1;
            }
            System.out.println("小哈手牌：" + Arrays.toString(xiaoha.getData()));
            System.out.println("桌上的牌："+Arrays.toString(ms.getData()));
            System.out.println("桌面top"+ms.getTop());
            //小哼出牌；同小哈出牌
            int xhhTemp = xiaoheng.out();
            if (book[xhhTemp] > 0) {
                xiaoheng.in(xhhTemp);
                while (ms.getTopData() != xhhTemp){
                    book[ms.getTopData()] = 0;
                    xiaoheng.in(ms.pop());
                }
            }else{
                ms.push(xhhTemp);
                book[xhhTemp] = 1;
            }
            System.out.println("小哼手牌：" + Arrays.toString(xiaoheng.getData()));
            System.out.println("桌上的牌："+Arrays.toString(ms.getData()));
            System.out.println("桌面top"+ms.getTop());
            System.out.println("****************");

        }
        if(xiaoha.isEmpty()){
            System.out.println("小哈 win! ");

        }else{
            System.out.println("小哼 win! ");

        }
        System.out.println("小哈手牌：" + Arrays.toString(xiaoha.getData()));
        System.out.println("小哼手牌：" + Arrays.toString(xiaoheng.getData()));
        System.out.println("桌上的牌："+Arrays.toString(ms.getData()));
    }
}
